
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>附录四：Dockerfile 最佳实践 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="debug.html" />
    
    
    <link rel="prev" href="command/dockerd.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../CHANGELOG.md">
            
                <span>
            
                    
                    修订记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../CONTRIBUTING.md">
            
                <span>
            
                    
                    如何贡献
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../introduction/">
            
                <a href="../introduction/">
            
                    
                    Docker 简介
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../introduction/what.html">
            
                <a href="../introduction/what.html">
            
                    
                    什么是 Docker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../introduction/why.html">
            
                <a href="../introduction/why.html">
            
                    
                    为什么要用 Docker
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basic_concept/">
            
                <a href="../basic_concept/">
            
                    
                    基本概念
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../basic_concept/image.html">
            
                <a href="../basic_concept/image.html">
            
                    
                    镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../basic_concept/container.html">
            
                <a href="../basic_concept/container.html">
            
                    
                    容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../basic_concept/repository.html">
            
                <a href="../basic_concept/repository.html">
            
                    
                    仓库
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../install/">
            
                <a href="../install/">
            
                    
                    安装 Docker
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../install/ubuntu.html">
            
                <a href="../install/ubuntu.html">
            
                    
                    Ubuntu
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../install/debian.html">
            
                <a href="../install/debian.html">
            
                    
                    Debian
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../install/fedora.html">
            
                <a href="../install/fedora.html">
            
                    
                    Fedora
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../install/centos.html">
            
                <a href="../install/centos.html">
            
                    
                    CentOS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../install/raspberry-pi.html">
            
                <a href="../install/raspberry-pi.html">
            
                    
                    Raspberry Pi
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../install/mac.html">
            
                <a href="../install/mac.html">
            
                    
                    macOS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../install/windows.html">
            
                <a href="../install/windows.html">
            
                    
                    Windows 10
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../install/mirror.html">
            
                <a href="../install/mirror.html">
            
                    
                    镜像加速器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../install/experimental.html">
            
                <a href="../install/experimental.html">
            
                    
                    开启实验特性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../image/">
            
                <a href="../image/">
            
                    
                    使用镜像
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../image/pull.html">
            
                <a href="../image/pull.html">
            
                    
                    获取镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../image/list.html">
            
                <a href="../image/list.html">
            
                    
                    列出镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../image/rm.html">
            
                <a href="../image/rm.html">
            
                    
                    删除本地镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../image/commit.html">
            
                <a href="../image/commit.html">
            
                    
                    利用 commit 理解镜像构成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../image/build.html">
            
                <a href="../image/build.html">
            
                    
                    使用 Dockerfile 定制镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../image/dockerfile/">
            
                <a href="../image/dockerfile/">
            
                    
                    Dockerfile 指令详解
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.6.1" data-path="../image/dockerfile/copy.html">
            
                <a href="../image/dockerfile/copy.html">
            
                    
                    COPY 复制文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.2" data-path="../image/dockerfile/add.html">
            
                <a href="../image/dockerfile/add.html">
            
                    
                    ADD 更高级的复制文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.3" data-path="../image/dockerfile/cmd.html">
            
                <a href="../image/dockerfile/cmd.html">
            
                    
                    CMD 容器启动命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.4" data-path="../image/dockerfile/entrypoint.html">
            
                <a href="../image/dockerfile/entrypoint.html">
            
                    
                    ENTRYPOINT 入口点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.5" data-path="../image/dockerfile/env.html">
            
                <a href="../image/dockerfile/env.html">
            
                    
                    ENV 设置环境变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.6" data-path="../image/dockerfile/arg.html">
            
                <a href="../image/dockerfile/arg.html">
            
                    
                    ARG 构建参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.7" data-path="../image/dockerfile/volume.html">
            
                <a href="../image/dockerfile/volume.html">
            
                    
                    VOLUME 定义匿名卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.8" data-path="../image/dockerfile/expose.html">
            
                <a href="../image/dockerfile/expose.html">
            
                    
                    EXPOSE 暴露端口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.9" data-path="../image/dockerfile/workdir.html">
            
                <a href="../image/dockerfile/workdir.html">
            
                    
                    WORKDIR 指定工作目录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.10" data-path="../image/dockerfile/user.html">
            
                <a href="../image/dockerfile/user.html">
            
                    
                    USER 指定当前用户
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.11" data-path="../image/dockerfile/healthcheck.html">
            
                <a href="../image/dockerfile/healthcheck.html">
            
                    
                    HEALTHCHECK 健康检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.12" data-path="../image/dockerfile/onbuild.html">
            
                <a href="../image/dockerfile/onbuild.html">
            
                    
                    ONBUILD 为他人作嫁衣裳
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6.13" data-path="../image/dockerfile/references.html">
            
                <a href="../image/dockerfile/references.html">
            
                    
                    参考文档
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../image/multistage-builds/">
            
                <a href="../image/multistage-builds/">
            
                    
                    Dockerfile 多阶段构建
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.7.1" data-path="../image/multistage-builds/laravel.html">
            
                <a href="../image/multistage-builds/laravel.html">
            
                    
                    实战多阶段构建 Laravel 镜像
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../image/manifest.html">
            
                <a href="../image/manifest.html">
            
                    
                    构建多种系统架构支持的 Docker 镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../image/other.html">
            
                <a href="../image/other.html">
            
                    
                    其它制作镜像的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../image/internal.html">
            
                <a href="../image/internal.html">
            
                    
                    实现原理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../container/">
            
                <a href="../container/">
            
                    
                    操作容器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../container/run.html">
            
                <a href="../container/run.html">
            
                    
                    启动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../container/daemon.html">
            
                <a href="../container/daemon.html">
            
                    
                    守护态运行
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../container/stop.html">
            
                <a href="../container/stop.html">
            
                    
                    终止
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../container/attach_exec.html">
            
                <a href="../container/attach_exec.html">
            
                    
                    进入容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../container/import_export.html">
            
                <a href="../container/import_export.html">
            
                    
                    导出和导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../container/rm.html">
            
                <a href="../container/rm.html">
            
                    
                    删除
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../repository/">
            
                <a href="../repository/">
            
                    
                    访问仓库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../repository/dockerhub.html">
            
                <a href="../repository/dockerhub.html">
            
                    
                    Docker Hub
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../repository/registry.html">
            
                <a href="../repository/registry.html">
            
                    
                    私有仓库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../repository/registry_auth.html">
            
                <a href="../repository/registry_auth.html">
            
                    
                    私有仓库高级配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../repository/nexus3_registry.html">
            
                <a href="../repository/nexus3_registry.html">
            
                    
                    Nexus 3
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../data_management/">
            
                <a href="../data_management/">
            
                    
                    数据管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../data_management/volume.html">
            
                <a href="../data_management/volume.html">
            
                    
                    数据卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../data_management/bind-mounts.html">
            
                <a href="../data_management/bind-mounts.html">
            
                    
                    挂载主机目录
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../network/">
            
                <a href="../network/">
            
                    
                    使用网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../network/port_mapping.html">
            
                <a href="../network/port_mapping.html">
            
                    
                    外部访问容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../network/linking.html">
            
                <a href="../network/linking.html">
            
                    
                    容器互联
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../network/dns.html">
            
                <a href="../network/dns.html">
            
                    
                    配置 DNS
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../advanced_network/">
            
                <a href="../advanced_network/">
            
                    
                    高级网络配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../advanced_network/quick_guide.html">
            
                <a href="../advanced_network/quick_guide.html">
            
                    
                    快速配置指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../advanced_network/access_control.html">
            
                <a href="../advanced_network/access_control.html">
            
                    
                    容器访问控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../advanced_network/port_mapping.html">
            
                <a href="../advanced_network/port_mapping.html">
            
                    
                    端口映射实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../advanced_network/docker0.html">
            
                <a href="../advanced_network/docker0.html">
            
                    
                    配置 docker0 网桥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../advanced_network/bridge.html">
            
                <a href="../advanced_network/bridge.html">
            
                    
                    自定义网桥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../advanced_network/example.html">
            
                <a href="../advanced_network/example.html">
            
                    
                    工具和示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.7" data-path="../advanced_network/config_file.html">
            
                <a href="../advanced_network/config_file.html">
            
                    
                    编辑网络配置文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.8" data-path="../advanced_network/ptp.html">
            
                <a href="../advanced_network/ptp.html">
            
                    
                    实例：创建一个点到点连接
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../buildx/">
            
                <a href="../buildx/">
            
                    
                    Docker Buildx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../buildx/buildkit.html">
            
                <a href="../buildx/buildkit.html">
            
                    
                    BuildKit
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../buildx/buildx.html">
            
                <a href="../buildx/buildx.html">
            
                    
                    使用 buildx 构建镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../buildx/multi-arch-images.html">
            
                <a href="../buildx/multi-arch-images.html">
            
                    
                    使用 buildx 构建多种系统架构支持的 Docker 镜像
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../compose/">
            
                <a href="../compose/">
            
                    
                    Docker Compose
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../compose/introduction.html">
            
                <a href="../compose/introduction.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../compose/install.html">
            
                <a href="../compose/install.html">
            
                    
                    安装与卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../compose/usage.html">
            
                <a href="../compose/usage.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../compose/commands.html">
            
                <a href="../compose/commands.html">
            
                    
                    命令说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../compose/compose_file.html">
            
                <a href="../compose/compose_file.html">
            
                    
                    Compose 模板文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.6" data-path="../compose/django.html">
            
                <a href="../compose/django.html">
            
                    
                    实战 Django
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.7" data-path="../compose/rails.html">
            
                <a href="../compose/rails.html">
            
                    
                    实战 Rails
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.8" data-path="../compose/wordpress.html">
            
                <a href="../compose/wordpress.html">
            
                    
                    实战 WordPress
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.9" data-path="../compose/lnmp.html">
            
                <a href="../compose/lnmp.html">
            
                    
                    实战 LNMP
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../swarm_mode/">
            
                <a href="../swarm_mode/">
            
                    
                    Swarm mode
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../swarm_mode/overview.html">
            
                <a href="../swarm_mode/overview.html">
            
                    
                    基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../swarm_mode/create.html">
            
                <a href="../swarm_mode/create.html">
            
                    
                    创建 Swarm 集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../swarm_mode/deploy.html">
            
                <a href="../swarm_mode/deploy.html">
            
                    
                    部署服务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.4" data-path="../swarm_mode/stack.html">
            
                <a href="../swarm_mode/stack.html">
            
                    
                    使用 compose 文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.5" data-path="../swarm_mode/secret.html">
            
                <a href="../swarm_mode/secret.html">
            
                    
                    管理密钥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.6" data-path="../swarm_mode/config.html">
            
                <a href="../swarm_mode/config.html">
            
                    
                    管理配置信息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.7" data-path="../swarm_mode/rolling_update.html">
            
                <a href="../swarm_mode/rolling_update.html">
            
                    
                    滚动升级
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../security/">
            
                <a href="../security/">
            
                    
                    安全
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../security/kernel_ns.html">
            
                <a href="../security/kernel_ns.html">
            
                    
                    内核命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../security/control_group.html">
            
                <a href="../security/control_group.html">
            
                    
                    控制组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../security/daemon_sec.html">
            
                <a href="../security/daemon_sec.html">
            
                    
                    服务端防护
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.4" data-path="../security/kernel_capability.html">
            
                <a href="../security/kernel_capability.html">
            
                    
                    内核能力机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.5" data-path="../security/other_feature.html">
            
                <a href="../security/other_feature.html">
            
                    
                    其它安全特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.6" data-path="../security/summary.html">
            
                <a href="../security/summary.html">
            
                    
                    总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../underly/">
            
                <a href="../underly/">
            
                    
                    底层实现
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../underly/arch.html">
            
                <a href="../underly/arch.html">
            
                    
                    基本架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../underly/namespace.html">
            
                <a href="../underly/namespace.html">
            
                    
                    命名空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../underly/cgroups.html">
            
                <a href="../underly/cgroups.html">
            
                    
                    控制组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../underly/ufs.html">
            
                <a href="../underly/ufs.html">
            
                    
                    联合文件系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.5" data-path="../underly/container_format.html">
            
                <a href="../underly/container_format.html">
            
                    
                    容器格式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.6" data-path="../underly/network.html">
            
                <a href="../underly/network.html">
            
                    
                    网络
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../etcd/">
            
                <a href="../etcd/">
            
                    
                    Etcd 项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../etcd/intro.html">
            
                <a href="../etcd/intro.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../etcd/install.html">
            
                <a href="../etcd/install.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../etcd/cluster.html">
            
                <a href="../etcd/cluster.html">
            
                    
                    集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.4" data-path="../etcd/etcdctl.html">
            
                <a href="../etcd/etcdctl.html">
            
                    
                    使用 etcdctl
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../coreos/">
            
                <a href="../coreos/">
            
                    
                    Fedora CoreOS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../coreos/intro.html">
            
                <a href="../coreos/intro.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.2" data-path="../coreos/install.html">
            
                <a href="../coreos/install.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../kubernetes/">
            
                <a href="../kubernetes/">
            
                    
                    Kubernetes - 开源容器编排引擎
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.20.1" data-path="../kubernetes/intro.html">
            
                <a href="../kubernetes/intro.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20.2" data-path="../kubernetes/concepts.html">
            
                <a href="../kubernetes/concepts.html">
            
                    
                    基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20.3" data-path="../kubernetes/design.html">
            
                <a href="../kubernetes/design.html">
            
                    
                    架构设计
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../kubernetes/setup/">
            
                <a href="../kubernetes/setup/">
            
                    
                    部署 Kubernetes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.21.1" data-path="../kubernetes/setup/kubeadm.html">
            
                <a href="../kubernetes/setup/kubeadm.html">
            
                    
                    使用 kubeadm 部署 kubernetes
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21.2" data-path="../kubernetes/setup/docker-desktop.html">
            
                <a href="../kubernetes/setup/docker-desktop.html">
            
                    
                    在 Docker Desktop 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21.3" data-path="../kubernetes/setup/systemd.html">
            
                <a href="../kubernetes/setup/systemd.html">
            
                    
                    一步步部署 kubernetes 集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21.4" data-path="../kubernetes/setup/dashboard.html">
            
                <a href="../kubernetes/setup/dashboard.html">
            
                    
                    部署 Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../kubernetes/kubectl/">
            
                <a href="../kubernetes/kubectl/">
            
                    
                    Kubernetes 命令行 kubectl
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../cloud/">
            
                <a href="../cloud/">
            
                    
                    容器与云计算
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.23.1" data-path="../cloud/intro.html">
            
                <a href="../cloud/intro.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23.2" data-path="../cloud/tencentCloud.html">
            
                <a href="../cloud/tencentCloud.html">
            
                    
                    腾讯云
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23.3" data-path="../cloud/alicloud.html">
            
                <a href="../cloud/alicloud.html">
            
                    
                    阿里云
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23.4" data-path="../cloud/aws.html">
            
                <a href="../cloud/aws.html">
            
                    
                    亚马逊云
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23.5" data-path="../cloud/summary.html">
            
                <a href="../cloud/summary.html">
            
                    
                    小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="../cases/os/">
            
                <a href="../cases/os/">
            
                    
                    实战案例 - 操作系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.24.1" data-path="../cases/os/busybox.html">
            
                <a href="../cases/os/busybox.html">
            
                    
                    Busybox
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24.2" data-path="../cases/os/alpine.html">
            
                <a href="../cases/os/alpine.html">
            
                    
                    Alpine
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24.3" data-path="../cases/os/debian.html">
            
                <a href="../cases/os/debian.html">
            
                    
                    Debian Ubuntu
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24.4" data-path="../cases/os/centos.html">
            
                <a href="../cases/os/centos.html">
            
                    
                    CentOS Fedora
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24.5" data-path="../cases/os/summary.html">
            
                <a href="../cases/os/summary.html">
            
                    
                    本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="../cases/ci/">
            
                <a href="../cases/ci/">
            
                    
                    实战案例 - CI/CD
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.25.1" data-path="../cases/ci/actions/">
            
                <a href="../cases/ci/actions/">
            
                    
                    GitHub Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25.2" data-path="../cases/ci/drone/">
            
                <a href="../cases/ci/drone/">
            
                    
                    Drone
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.25.2.1" data-path="../cases/ci/drone/install.html">
            
                <a href="../cases/ci/drone/install.html">
            
                    
                    部署 Drone
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.25.3" data-path="../cases/ci/travis/">
            
                <a href="../cases/ci/travis/">
            
                    
                    Travis CI
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="../IDE/">
            
                <a href="../IDE/">
            
                    
                    在 IDE 中使用 Docker
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.26.1" data-path="../IDE/vsCode.html">
            
                <a href="../IDE/vsCode.html">
            
                    
                    VS Code
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="../opensource/">
            
                <a href="../opensource/">
            
                    
                    Docker 开源项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.27.1" data-path="../opensource/linuxkit.html">
            
                <a href="../opensource/linuxkit.html">
            
                    
                    LinuxKit
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="../podman/">
            
                <a href="../podman/">
            
                    
                    podman - 下一代 Linux 容器工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="./">
            
                <a href="./">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.29.1" data-path="faq/">
            
                <a href="faq/">
            
                    
                    附录一：常见问题总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2" data-path="repo/">
            
                <a href="repo/">
            
                    
                    附录二：热门镜像介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.29.2.1" data-path="repo/ubuntu.html">
            
                <a href="repo/ubuntu.html">
            
                    
                    Ubuntu
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.2" data-path="repo/centos.html">
            
                <a href="repo/centos.html">
            
                    
                    CentOS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.3" data-path="repo/nginx.html">
            
                <a href="repo/nginx.html">
            
                    
                    Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.4" data-path="repo/php.html">
            
                <a href="repo/php.html">
            
                    
                    PHP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.5" data-path="repo/nodejs.html">
            
                <a href="repo/nodejs.html">
            
                    
                    Node.js
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.6" data-path="repo/mysql.html">
            
                <a href="repo/mysql.html">
            
                    
                    MySQL
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.7" data-path="repo/wordpress.html">
            
                <a href="repo/wordpress.html">
            
                    
                    WordPress
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.8" data-path="repo/mongodb.html">
            
                <a href="repo/mongodb.html">
            
                    
                    MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.2.9" data-path="repo/redis.html">
            
                <a href="repo/redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.29.3" data-path="command/">
            
                <a href="command/">
            
                    
                    附录三：Docker 命令查询
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.29.3.1" data-path="command/docker.html">
            
                <a href="command/docker.html">
            
                    
                    客户端命令 - docker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.3.2" data-path="command/dockerd.html">
            
                <a href="command/dockerd.html">
            
                    
                    服务端命令 - dockerd
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.29.4" data-path="best_practices.html">
            
                <a href="best_practices.html">
            
                    
                    附录四：Dockerfile 最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.5" data-path="debug.html">
            
                <a href="debug.html">
            
                    
                    附录五：如何调试 Docker
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29.6" data-path="resources.html">
            
                <a href="resources.html">
            
                    
                    附录六：资源链接
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="../archive/">
            
                <a href="../archive/">
            
                    
                    归档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.30.1" data-path="../mesos/">
            
                <a href="../mesos/">
            
                    
                    Mesos - 优秀的集群资源调度平台
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.30.1.1" data-path="../mesos/intro.html">
            
                <a href="../mesos/intro.html">
            
                    
                    Mesos 简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.2" data-path="../mesos/installation.html">
            
                <a href="../mesos/installation.html">
            
                    
                    安装与使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.3" data-path="../mesos/architecture.html">
            
                <a href="../mesos/architecture.html">
            
                    
                    原理与架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.4" data-path="../mesos/configuration.html">
            
                <a href="../mesos/configuration.html">
            
                    
                    Mesos 配置项解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.5" data-path="../mesos/monitor.html">
            
                <a href="../mesos/monitor.html">
            
                    
                    日志与监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.6" data-path="../mesos/framework.html">
            
                <a href="../mesos/framework.html">
            
                    
                    常见应用框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.1.7" data-path="../mesos/summary.html">
            
                <a href="../mesos/summary.html">
            
                    
                    本章小结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.30.2" data-path="../machine/">
            
                <a href="../machine/">
            
                    
                    Docker Machine
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.30.2.1" data-path="../machine/install.html">
            
                <a href="../machine/install.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30.2.2" data-path="../machine/usage.html">
            
                <a href="../machine/usage.html">
            
                    
                    使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.30.3" data-path="../swarm/">
            
                <a href="../swarm/">
            
                    
                    Docker Swarm
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >附录四：Dockerfile 最佳实践</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="dockerfile-&#x6700;&#x4F73;&#x5B9E;&#x8DF5;">Dockerfile &#x6700;&#x4F73;&#x5B9E;&#x8DF5;</h1>
<p>&#x672C;&#x9644;&#x5F55;&#x662F;&#x7B14;&#x8005;&#x5BF9; Docker &#x5B98;&#x65B9;&#x6587;&#x6863;&#x4E2D; <a href="https://docs.docker.com/develop/develop-images/dockerfile_best-practices/" target="_blank">Best practices for writing Dockerfiles</a> &#x7684;&#x7406;&#x89E3;&#x4E0E;&#x7FFB;&#x8BD1;&#x3002;</p>
<h2 id="&#x4E00;&#x822C;&#x6027;&#x7684;&#x6307;&#x5357;&#x548C;&#x5EFA;&#x8BAE;">&#x4E00;&#x822C;&#x6027;&#x7684;&#x6307;&#x5357;&#x548C;&#x5EFA;&#x8BAE;</h2>
<h3 id="&#x5BB9;&#x5668;&#x5E94;&#x8BE5;&#x662F;&#x77ED;&#x6682;&#x7684;">&#x5BB9;&#x5668;&#x5E94;&#x8BE5;&#x662F;&#x77ED;&#x6682;&#x7684;</h3>
<p>&#x901A;&#x8FC7; <code>Dockerfile</code> &#x6784;&#x5EFA;&#x7684;&#x955C;&#x50CF;&#x6240;&#x542F;&#x52A8;&#x7684;&#x5BB9;&#x5668;&#x5E94;&#x8BE5;&#x5C3D;&#x53EF;&#x80FD;&#x77ED;&#x6682;&#xFF08;&#x751F;&#x547D;&#x5468;&#x671F;&#x77ED;&#xFF09;&#x3002;&#x300C;&#x77ED;&#x6682;&#x300D;&#x610F;&#x5473;&#x7740;&#x53EF;&#x4EE5;&#x505C;&#x6B62;&#x548C;&#x9500;&#x6BC1;&#x5BB9;&#x5668;&#xFF0C;&#x5E76;&#x4E14;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x5BB9;&#x5668;&#x5E76;&#x90E8;&#x7F72;&#x597D;&#x6240;&#x9700;&#x7684;&#x8BBE;&#x7F6E;&#x548C;&#x914D;&#x7F6E;&#x5DE5;&#x4F5C;&#x91CF;&#x5E94;&#x8BE5;&#x662F;&#x6781;&#x5C0F;&#x7684;&#x3002;</p>
<h3 id="&#x4F7F;&#x7528;-dockerignore-&#x6587;&#x4EF6;">&#x4F7F;&#x7528; <code>.dockerignore</code> &#x6587;&#x4EF6;</h3>
<p>&#x4F7F;&#x7528; <code>Dockerfile</code> &#x6784;&#x5EFA;&#x955C;&#x50CF;&#x65F6;&#x6700;&#x597D;&#x662F;&#x5C06; <code>Dockerfile</code> &#x653E;&#x7F6E;&#x5728;&#x4E00;&#x4E2A;&#x65B0;&#x5EFA;&#x7684;&#x7A7A;&#x76EE;&#x5F55;&#x4E0B;&#x3002;&#x7136;&#x540E;&#x5C06;&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x6240;&#x9700;&#x8981;&#x7684;&#x6587;&#x4EF6;&#x6DFB;&#x52A0;&#x5230;&#x8BE5;&#x76EE;&#x5F55;&#x4E2D;&#x3002;&#x4E3A;&#x4E86;&#x63D0;&#x9AD8;&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x7684;&#x6548;&#x7387;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x76EE;&#x5F55;&#x4E0B;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A; <code>.dockerignore</code> &#x6587;&#x4EF6;&#x6765;&#x6307;&#x5B9A;&#x8981;&#x5FFD;&#x7565;&#x7684;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;&#x3002;<code>.dockerignore</code> &#x6587;&#x4EF6;&#x7684;&#x6392;&#x9664;&#x6A21;&#x5F0F;&#x8BED;&#x6CD5;&#x548C; Git &#x7684; <code>.gitignore</code> &#x6587;&#x4EF6;&#x76F8;&#x4F3C;&#x3002;</p>
<h3 id="&#x4F7F;&#x7528;&#x591A;&#x9636;&#x6BB5;&#x6784;&#x5EFA;">&#x4F7F;&#x7528;&#x591A;&#x9636;&#x6BB5;&#x6784;&#x5EFA;</h3>
<p>&#x5728; <code>Docker 17.05</code> &#x4EE5;&#x4E0A;&#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="../image/multistage-builds.md">&#x591A;&#x9636;&#x6BB5;&#x6784;&#x5EFA;</a> &#x6765;&#x51CF;&#x5C11;&#x6240;&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x7684;&#x5927;&#x5C0F;&#x3002;</p>
<h3 id="&#x907F;&#x514D;&#x5B89;&#x88C5;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5305;">&#x907F;&#x514D;&#x5B89;&#x88C5;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5305;</h3>
<p>&#x4E3A;&#x4E86;&#x964D;&#x4F4E;&#x590D;&#x6742;&#x6027;&#x3001;&#x51CF;&#x5C11;&#x4F9D;&#x8D56;&#x3001;&#x51CF;&#x5C0F;&#x6587;&#x4EF6;&#x5927;&#x5C0F;&#x3001;&#x8282;&#x7EA6;&#x6784;&#x5EFA;&#x65F6;&#x95F4;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x5B89;&#x88C5;&#x4EFB;&#x4F55;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x5305;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E0D;&#x8981;&#x5728;&#x6570;&#x636E;&#x5E93;&#x955C;&#x50CF;&#x4E2D;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x6587;&#x672C;&#x7F16;&#x8F91;&#x5668;&#x3002;</p>
<h3 id="&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;">&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;</h3>
<p>&#x5E94;&#x8BE5;&#x4FDD;&#x8BC1;&#x5728;&#x4E00;&#x4E2A;&#x5BB9;&#x5668;&#x4E2D;&#x53EA;&#x8FD0;&#x884C;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x3002;&#x5C06;&#x591A;&#x4E2A;&#x5E94;&#x7528;&#x89E3;&#x8026;&#x5230;&#x4E0D;&#x540C;&#x5BB9;&#x5668;&#x4E2D;&#xFF0C;&#x4FDD;&#x8BC1;&#x4E86;&#x5BB9;&#x5668;&#x7684;&#x6A2A;&#x5411;&#x6269;&#x5C55;&#x548C;&#x590D;&#x7528;&#x3002;&#x4F8B;&#x5982; web &#x5E94;&#x7528;&#x5E94;&#x8BE5;&#x5305;&#x542B;&#x4E09;&#x4E2A;&#x5BB9;&#x5668;&#xFF1A;web&#x5E94;&#x7528;&#x3001;&#x6570;&#x636E;&#x5E93;&#x3001;&#x7F13;&#x5B58;&#x3002;</p>
<p>&#x5982;&#x679C;&#x5BB9;&#x5668;&#x4E92;&#x76F8;&#x4F9D;&#x8D56;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="../network/linking.html">Docker &#x81EA;&#x5B9A;&#x4E49;&#x7F51;&#x7EDC;</a> &#x6765;&#x628A;&#x8FD9;&#x4E9B;&#x5BB9;&#x5668;&#x8FDE;&#x63A5;&#x8D77;&#x6765;&#x3002;</p>
<h3 id="&#x955C;&#x50CF;&#x5C42;&#x6570;&#x5C3D;&#x53EF;&#x80FD;&#x5C11;">&#x955C;&#x50CF;&#x5C42;&#x6570;&#x5C3D;&#x53EF;&#x80FD;&#x5C11;</h3>
<p>&#x4F60;&#x9700;&#x8981;&#x5728; <code>Dockerfile</code> &#x53EF;&#x8BFB;&#x6027;&#xFF08;&#x4E5F;&#x5305;&#x62EC;&#x957F;&#x671F;&#x7684;&#x53EF;&#x7EF4;&#x62A4;&#x6027;&#xFF09;&#x548C;&#x51CF;&#x5C11;&#x5C42;&#x6570;&#x4E4B;&#x95F4;&#x505A;&#x4E00;&#x4E2A;&#x5E73;&#x8861;&#x3002;</p>
<h3 id="&#x5C06;&#x591A;&#x884C;&#x53C2;&#x6570;&#x6392;&#x5E8F;">&#x5C06;&#x591A;&#x884C;&#x53C2;&#x6570;&#x6392;&#x5E8F;</h3>
<p>&#x5C06;&#x591A;&#x884C;&#x53C2;&#x6570;&#x6309;&#x5B57;&#x6BCD;&#x987A;&#x5E8F;&#x6392;&#x5E8F;&#xFF08;&#x6BD4;&#x5982;&#x8981;&#x5B89;&#x88C5;&#x591A;&#x4E2A;&#x5305;&#x65F6;&#xFF09;&#x3002;&#x8FD9;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x4F60;&#x907F;&#x514D;&#x91CD;&#x590D;&#x5305;&#x542B;&#x540C;&#x4E00;&#x4E2A;&#x5305;&#xFF0C;&#x66F4;&#x65B0;&#x5305;&#x5217;&#x8868;&#x65F6;&#x4E5F;&#x66F4;&#x5BB9;&#x6613;&#x3002;&#x4E5F;&#x4FBF;&#x4E8E; <code>PRs</code> &#x9605;&#x8BFB;&#x548C;&#x5BA1;&#x67E5;&#x3002;&#x5EFA;&#x8BAE;&#x5728;&#x53CD;&#x659C;&#x6760;&#x7B26;&#x53F7; <code>\</code> &#x4E4B;&#x524D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x7A7A;&#x683C;&#xFF0C;&#x4EE5;&#x589E;&#x52A0;&#x53EF;&#x8BFB;&#x6027;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x6765;&#x81EA; <code>buildpack-deps</code> &#x955C;&#x50CF;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">RUN</span> <span class="bash">apt-get update &amp;&amp; apt-get install -y \
  bzr \
  cvs \
  git \
  mercurial \
  subversion
</span></code></pre>
<h3 id="&#x6784;&#x5EFA;&#x7F13;&#x5B58;">&#x6784;&#x5EFA;&#x7F13;&#x5B58;</h3>
<p>&#x5728;&#x955C;&#x50CF;&#x7684;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;Docker &#x4F1A;&#x904D;&#x5386; <code>Dockerfile</code> &#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x6307;&#x4EE4;&#xFF0C;&#x7136;&#x540E;&#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C;&#x3002;&#x5728;&#x6267;&#x884C;&#x6BCF;&#x6761;&#x6307;&#x4EE4;&#x4E4B;&#x524D;&#xFF0C;Docker &#x90FD;&#x4F1A;&#x5728;&#x7F13;&#x5B58;&#x4E2D;&#x67E5;&#x627E;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x53EF;&#x91CD;&#x7528;&#x7684;&#x955C;&#x50CF;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5C31;&#x4F7F;&#x7528;&#x73B0;&#x5B58;&#x7684;&#x955C;&#x50CF;&#xFF0C;&#x4E0D;&#x518D;&#x91CD;&#x590D;&#x521B;&#x5EFA;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x60F3;&#x5728;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728; <code>docker build</code> &#x547D;&#x4EE4;&#x4E2D;&#x4F7F;&#x7528; <code>--no-cache=true</code> &#x9009;&#x9879;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x6784;&#x5EFA;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F7F;&#x7528;&#x7F13;&#x5B58;&#xFF0C;&#x4F60;&#x5F97;&#x660E;&#x767D;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4F1A;&#xFF0C;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4E0D;&#x4F1A;&#x627E;&#x5230;&#x5339;&#x914D;&#x7684;&#x955C;&#x50CF;&#xFF0C;&#x9075;&#x5FAA;&#x7684;&#x57FA;&#x672C;&#x89C4;&#x5219;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li>&#x4ECE;&#x4E00;&#x4E2A;&#x57FA;&#x7840;&#x955C;&#x50CF;&#x5F00;&#x59CB;&#xFF08;<code>FROM</code> &#x6307;&#x4EE4;&#x6307;&#x5B9A;&#xFF09;&#xFF0C;&#x4E0B;&#x4E00;&#x6761;&#x6307;&#x4EE4;&#x5C06;&#x548C;&#x8BE5;&#x57FA;&#x7840;&#x955C;&#x50CF;&#x7684;&#x6240;&#x6709;&#x5B50;&#x955C;&#x50CF;&#x8FDB;&#x884C;&#x5339;&#x914D;&#xFF0C;&#x68C0;&#x67E5;&#x8FD9;&#x4E9B;&#x5B50;&#x955C;&#x50CF;&#x88AB;&#x521B;&#x5EFA;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x6307;&#x4EE4;&#x662F;&#x5426;&#x548C;&#x88AB;&#x68C0;&#x67E5;&#x7684;&#x6307;&#x4EE4;&#x5B8C;&#x5168;&#x4E00;&#x6837;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x5219;&#x7F13;&#x5B58;&#x5931;&#x6548;&#x3002;</li>
<li>&#x5728;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x7B80;&#x5355;&#x5730;&#x5BF9;&#x6BD4; <code>Dockerfile</code> &#x4E2D;&#x7684;&#x6307;&#x4EE4;&#x548C;&#x5B50;&#x955C;&#x50CF;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x6709;&#x4E9B;&#x6307;&#x4EE4;&#x9700;&#x8981;&#x66F4;&#x591A;&#x7684;&#x68C0;&#x67E5;&#x548C;&#x89E3;&#x91CA;&#x3002;</li>
<li>&#x5BF9;&#x4E8E; <code>ADD</code> &#x548C; <code>COPY</code> &#x6307;&#x4EE4;&#xFF0C;&#x955C;&#x50CF;&#x4E2D;&#x5BF9;&#x5E94;&#x6587;&#x4EF6;&#x7684;&#x5185;&#x5BB9;&#x4E5F;&#x4F1A;&#x88AB;&#x68C0;&#x67E5;&#xFF0C;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x90FD;&#x4F1A;&#x8BA1;&#x7B97;&#x51FA;&#x4E00;&#x4E2A;&#x6821;&#x9A8C;&#x548C;&#x3002;&#x6587;&#x4EF6;&#x7684;&#x6700;&#x540E;&#x4FEE;&#x6539;&#x65F6;&#x95F4;&#x548C;&#x6700;&#x540E;&#x8BBF;&#x95EE;&#x65F6;&#x95F4;&#x4E0D;&#x4F1A;&#x7EB3;&#x5165;&#x6821;&#x9A8C;&#x3002;&#x5728;&#x7F13;&#x5B58;&#x7684;&#x67E5;&#x627E;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x4F1A;&#x5C06;&#x8FD9;&#x4E9B;&#x6821;&#x9A8C;&#x548C;&#x548C;&#x5DF2;&#x5B58;&#x5728;&#x955C;&#x50CF;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x6821;&#x9A8C;&#x548C;&#x8FDB;&#x884C;&#x5BF9;&#x6BD4;&#x3002;&#x5982;&#x679C;&#x6587;&#x4EF6;&#x6709;&#x4EFB;&#x4F55;&#x6539;&#x53D8;&#xFF0C;&#x6BD4;&#x5982;&#x5185;&#x5BB9;&#x548C;&#x5143;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x7F13;&#x5B58;&#x5931;&#x6548;&#x3002;</li>
<li>&#x9664;&#x4E86; <code>ADD</code> &#x548C; <code>COPY</code> &#x6307;&#x4EE4;&#xFF0C;&#x7F13;&#x5B58;&#x5339;&#x914D;&#x8FC7;&#x7A0B;&#x4E0D;&#x4F1A;&#x67E5;&#x770B;&#x4E34;&#x65F6;&#x5BB9;&#x5668;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x6765;&#x51B3;&#x5B9A;&#x7F13;&#x5B58;&#x662F;&#x5426;&#x5339;&#x914D;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x6267;&#x884C;&#x5B8C; <code>RUN apt-get -y update</code> &#x6307;&#x4EE4;&#x540E;&#xFF0C;&#x5BB9;&#x5668;&#x4E2D;&#x4E00;&#x4E9B;&#x6587;&#x4EF6;&#x88AB;&#x66F4;&#x65B0;&#xFF0C;&#x4F46; Docker &#x4E0D;&#x4F1A;&#x68C0;&#x67E5;&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EA;&#x6709;&#x6307;&#x4EE4;&#x5B57;&#x7B26;&#x4E32;&#x672C;&#x8EAB;&#x88AB;&#x7528;&#x6765;&#x5339;&#x914D;&#x7F13;&#x5B58;&#x3002;</li>
</ul>
<p>&#x4E00;&#x65E6;&#x7F13;&#x5B58;&#x5931;&#x6548;&#xFF0C;&#x6240;&#x6709;&#x540E;&#x7EED;&#x7684; <code>Dockerfile</code> &#x6307;&#x4EE4;&#x90FD;&#x5C06;&#x4EA7;&#x751F;&#x65B0;&#x7684;&#x955C;&#x50CF;&#xFF0C;&#x7F13;&#x5B58;&#x4E0D;&#x4F1A;&#x88AB;&#x4F7F;&#x7528;&#x3002;</p>
<h2 id="dockerfile-&#x6307;&#x4EE4;">Dockerfile &#x6307;&#x4EE4;</h2>
<p>&#x4E0B;&#x9762;&#x9488;&#x5BF9; <code>Dockerfile</code> &#x4E2D;&#x5404;&#x79CD;&#x6307;&#x4EE4;&#x7684;&#x6700;&#x4F73;&#x7F16;&#x5199;&#x65B9;&#x5F0F;&#x7ED9;&#x51FA;&#x5EFA;&#x8BAE;&#x3002;</p>
<h3 id="from">FROM</h3>
<p>&#x5C3D;&#x53EF;&#x80FD;&#x4F7F;&#x7528;&#x5F53;&#x524D;&#x5B98;&#x65B9;&#x4ED3;&#x5E93;&#x4F5C;&#x4E3A;&#x4F60;&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x7684;&#x57FA;&#x7840;&#x3002;&#x63A8;&#x8350;&#x4F7F;&#x7528; <a href="https://hub.docker.com/_/alpine/" target="_blank">Alpine</a> &#x955C;&#x50CF;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x88AB;&#x4E25;&#x683C;&#x63A7;&#x5236;&#x5E76;&#x4FDD;&#x6301;&#x6700;&#x5C0F;&#x5C3A;&#x5BF8;&#xFF08;&#x76EE;&#x524D;&#x5C0F;&#x4E8E; 5 MB&#xFF09;&#xFF0C;&#x4F46;&#x5B83;&#x4ECD;&#x7136;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x53D1;&#x884C;&#x7248;&#x3002;</p>
<h3 id="label">LABEL</h3>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x7ED9;&#x955C;&#x50CF;&#x6DFB;&#x52A0;&#x6807;&#x7B7E;&#x6765;&#x5E2E;&#x52A9;&#x7EC4;&#x7EC7;&#x955C;&#x50CF;&#x3001;&#x8BB0;&#x5F55;&#x8BB8;&#x53EF;&#x4FE1;&#x606F;&#x3001;&#x8F85;&#x52A9;&#x81EA;&#x52A8;&#x5316;&#x6784;&#x5EFA;&#x7B49;&#x3002;&#x6BCF;&#x4E2A;&#x6807;&#x7B7E;&#x4E00;&#x884C;&#xFF0C;&#x7531; <code>LABEL</code> &#x5F00;&#x5934;&#x52A0;&#x4E0A;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6807;&#x7B7E;&#x5BF9;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x5C55;&#x793A;&#x4E86;&#x5404;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x53EF;&#x80FD;&#x683C;&#x5F0F;&#x3002;<code>#</code> &#x5F00;&#x5934;&#x7684;&#x884C;&#x662F;&#x6CE8;&#x91CA;&#x5185;&#x5BB9;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x4F60;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x5305;&#x542B;&#x7A7A;&#x683C;&#xFF0C;&#x5FC5;&#x987B;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x653E;&#x5165;&#x5F15;&#x53F7;&#x4E2D;&#x6216;&#x8005;&#x5BF9;&#x7A7A;&#x683C;&#x4F7F;&#x7528;&#x8F6C;&#x4E49;&#x3002;&#x5982;&#x679C;&#x5B57;&#x7B26;&#x4E32;&#x5185;&#x5BB9;&#x672C;&#x8EAB;&#x5C31;&#x5305;&#x542B;&#x5F15;&#x53F7;&#xFF0C;&#x5FC5;&#x987B;&#x5BF9;&#x5F15;&#x53F7;&#x4F7F;&#x7528;&#x8F6C;&#x4E49;&#x3002;</p>
</blockquote>
<pre><code class="lang-docker"><span class="hljs-comment"># Set one or more individual labels</span>
<span class="hljs-keyword">LABEL</span> <span class="bash">com.example.version=<span class="hljs-string">&quot;0.0.1-beta&quot;</span>
</span>
<span class="hljs-keyword">LABEL</span> <span class="bash">vendor=<span class="hljs-string">&quot;ACME Incorporated&quot;</span>
</span>
<span class="hljs-keyword">LABEL</span> <span class="bash">com.example.release-date=<span class="hljs-string">&quot;2015-02-12&quot;</span>
</span>
<span class="hljs-keyword">LABEL</span> <span class="bash">com.example.version.is-production=<span class="hljs-string">&quot;&quot;</span>
</span></code></pre>
<p>&#x4E00;&#x4E2A;&#x955C;&#x50CF;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x4F46;&#x5EFA;&#x8BAE;&#x5C06;&#x591A;&#x4E2A;&#x6807;&#x7B7E;&#x653E;&#x5165;&#x5230;&#x4E00;&#x4E2A; <code>LABEL</code> &#x6307;&#x4EE4;&#x4E2D;&#x3002;</p>
<pre><code class="lang-docker"><span class="hljs-comment"># Set multiple labels at once, using line-continuation characters to break long lines</span>
<span class="hljs-keyword">LABEL</span> <span class="bash">vendor=ACME\ Incorporated \
      com.example.is-beta= \
      com.example.is-production=<span class="hljs-string">&quot;&quot;</span> \
      com.example.version=<span class="hljs-string">&quot;0.0.1-beta&quot;</span> \
      com.example.release-date=<span class="hljs-string">&quot;2015-02-12&quot;</span>
</span></code></pre>
<p>&#x5173;&#x4E8E;&#x6807;&#x7B7E;&#x53EF;&#x4EE5;&#x63A5;&#x53D7;&#x7684;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x53C2;&#x8003; <a href="https://docs.docker.com/config/labels-custom-metadata/" target="_blank">Understanding object labels</a>&#x3002;&#x5173;&#x4E8E;&#x67E5;&#x8BE2;&#x6807;&#x7B7E;&#x4FE1;&#x606F;&#xFF0C;&#x53C2;&#x8003; <a href="https://docs.docker.com/config/labels-custom-metadata/" target="_blank">Managing labels on objects</a>&#x3002;</p>
<h3 id="run">RUN</h3>
<p>&#x4E3A;&#x4E86;&#x4FDD;&#x6301; <code>Dockerfile</code> &#x6587;&#x4EF6;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#xFF0C;&#x53EF;&#x7406;&#x89E3;&#x6027;&#xFF0C;&#x4EE5;&#x53CA;&#x53EF;&#x7EF4;&#x62A4;&#x6027;&#xFF0C;&#x5EFA;&#x8BAE;&#x5C06;&#x957F;&#x7684;&#x6216;&#x590D;&#x6742;&#x7684; <code>RUN</code> &#x6307;&#x4EE4;&#x7528;&#x53CD;&#x659C;&#x6760; <code>\</code> &#x5206;&#x5272;&#x6210;&#x591A;&#x884C;&#x3002;</p>
<h4 id="apt-get">apt-get</h4>
<p><code>RUN</code> &#x6307;&#x4EE4;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x662F;&#x5B89;&#x88C5;&#x5305;&#x7528;&#x7684; <code>apt-get</code>&#x3002;&#x56E0;&#x4E3A; <code>RUN apt-get</code> &#x6307;&#x4EE4;&#x4F1A;&#x5B89;&#x88C5;&#x5305;&#xFF0C;&#x6240;&#x4EE5;&#x6709;&#x51E0;&#x4E2A;&#x95EE;&#x9898;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x3002;</p>
<p>&#x4E0D;&#x8981;&#x4F7F;&#x7528; <code>RUN apt-get upgrade</code> &#x6216; <code>dist-upgrade</code>&#xFF0C;&#x56E0;&#x4E3A;&#x8BB8;&#x591A;&#x57FA;&#x7840;&#x955C;&#x50CF;&#x4E2D;&#x7684;&#x300C;&#x5FC5;&#x987B;&#x300D;&#x5305;&#x4E0D;&#x4F1A;&#x5728;&#x4E00;&#x4E2A;&#x975E;&#x7279;&#x6743;&#x5BB9;&#x5668;&#x4E2D;&#x5347;&#x7EA7;&#x3002;&#x5982;&#x679C;&#x57FA;&#x7840;&#x955C;&#x50CF;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x5305;&#x8FC7;&#x65F6;&#x4E86;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x8054;&#x7CFB;&#x5B83;&#x7684;&#x7EF4;&#x62A4;&#x8005;&#x3002;&#x5982;&#x679C;&#x4F60;&#x786E;&#x5B9A;&#x67D0;&#x4E2A;&#x7279;&#x5B9A;&#x7684;&#x5305;&#xFF0C;&#x6BD4;&#x5982; <code>foo</code>&#xFF0C;&#x9700;&#x8981;&#x5347;&#x7EA7;&#xFF0C;&#x4F7F;&#x7528; <code>apt-get install -y foo</code> &#x5C31;&#x884C;&#xFF0C;&#x8BE5;&#x6307;&#x4EE4;&#x4F1A;&#x81EA;&#x52A8;&#x5347;&#x7EA7; <code>foo</code> &#x5305;&#x3002;</p>
<p>&#x6C38;&#x8FDC;&#x5C06; <code>RUN apt-get update</code> &#x548C; <code>apt-get install</code> &#x7EC4;&#x5408;&#x6210;&#x4E00;&#x6761; <code>RUN</code> &#x58F0;&#x660E;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">RUN</span> <span class="bash">apt-get update &amp;&amp; apt-get install -y \
        package-bar \
        package-baz \
        package-foo
</span></code></pre>
<p>&#x5C06; <code>apt-get update</code> &#x653E;&#x5728;&#x4E00;&#x6761;&#x5355;&#x72EC;&#x7684; <code>RUN</code> &#x58F0;&#x660E;&#x4E2D;&#x4F1A;&#x5BFC;&#x81F4;&#x7F13;&#x5B58;&#x95EE;&#x9898;&#x4EE5;&#x53CA;&#x540E;&#x7EED;&#x7684; <code>apt-get install</code> &#x5931;&#x8D25;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x5047;&#x8BBE;&#x4F60;&#x6709;&#x4E00;&#x4E2A; <code>Dockerfile</code> &#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">FROM</span> ubuntu:<span class="hljs-number">18.04</span>

<span class="hljs-keyword">RUN</span> <span class="bash">apt-get update
</span>
<span class="hljs-keyword">RUN</span> <span class="bash">apt-get install -y curl
</span></code></pre>
<p>&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x540E;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x5C42;&#x90FD;&#x5728; Docker &#x7684;&#x7F13;&#x5B58;&#x4E2D;&#x3002;&#x5047;&#x8BBE;&#x4F60;&#x540E;&#x6765;&#x53C8;&#x4FEE;&#x6539;&#x4E86;&#x5176;&#x4E2D;&#x7684; <code>apt-get install</code> &#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x5305;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">FROM</span> ubuntu:<span class="hljs-number">18.04</span>

<span class="hljs-keyword">RUN</span> <span class="bash">apt-get update
</span>
<span class="hljs-keyword">RUN</span> <span class="bash">apt-get install -y curl nginx
</span></code></pre>
<p>Docker &#x53D1;&#x73B0;&#x4FEE;&#x6539;&#x540E;&#x7684; <code>RUN apt-get update</code> &#x6307;&#x4EE4;&#x548C;&#x4E4B;&#x524D;&#x7684;&#x5B8C;&#x5168;&#x4E00;&#x6837;&#x3002;&#x6240;&#x4EE5;&#xFF0C;<code>apt-get update</code> &#x4E0D;&#x4F1A;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x662F;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x7684;&#x7F13;&#x5B58;&#x955C;&#x50CF;&#x3002;&#x56E0;&#x4E3A; <code>apt-get update</code> &#x6CA1;&#x6709;&#x8FD0;&#x884C;&#xFF0C;&#x540E;&#x9762;&#x7684; <code>apt-get install</code> &#x53EF;&#x80FD;&#x5B89;&#x88C5;&#x7684;&#x662F;&#x8FC7;&#x65F6;&#x7684; <code>curl</code> &#x548C; <code>nginx</code> &#x7248;&#x672C;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>RUN apt-get update &amp;&amp; apt-get install -y</code> &#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x4F60;&#x7684; Dockerfiles &#x6BCF;&#x6B21;&#x5B89;&#x88C5;&#x7684;&#x90FD;&#x662F;&#x5305;&#x7684;&#x6700;&#x65B0;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x4E0D;&#x9700;&#x8981;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x7F16;&#x7801;&#x6216;&#x989D;&#x5916;&#x5E72;&#x9884;&#x3002;&#x8FD9;&#x9879;&#x6280;&#x672F;&#x53EB;&#x4F5C; <code>cache busting</code>&#x3002;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x663E;&#x793A;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x5305;&#x7684;&#x7248;&#x672C;&#x53F7;&#x6765;&#x8FBE;&#x5230; <code>cache-busting</code>&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x6240;&#x8C13;&#x7684;&#x56FA;&#x5B9A;&#x7248;&#x672C;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">RUN</span> <span class="bash">apt-get update &amp;&amp; apt-get install -y \
    package-bar \
    package-baz \
    package-foo=1.3.*
</span></code></pre>
<p>&#x56FA;&#x5B9A;&#x7248;&#x672C;&#x4F1A;&#x8FEB;&#x4F7F;&#x6784;&#x5EFA;&#x8FC7;&#x7A0B;&#x68C0;&#x7D22;&#x7279;&#x5B9A;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x800C;&#x4E0D;&#x7BA1;&#x7F13;&#x5B58;&#x4E2D;&#x6709;&#x4EC0;&#x4E48;&#x3002;&#x8FD9;&#x9879;&#x6280;&#x672F;&#x4E5F;&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x56E0;&#x6240;&#x9700;&#x5305;&#x4E2D;&#x672A;&#x9884;&#x6599;&#x5230;&#x7684;&#x53D8;&#x5316;&#x800C;&#x5BFC;&#x81F4;&#x7684;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A; <code>RUN</code> &#x6307;&#x4EE4;&#x7684;&#x793A;&#x4F8B;&#x6A21;&#x677F;&#xFF0C;&#x5C55;&#x793A;&#x4E86;&#x6240;&#x6709;&#x5173;&#x4E8E; <code>apt-get</code> &#x7684;&#x5EFA;&#x8BAE;&#x3002;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">RUN</span> <span class="bash">apt-get update &amp;&amp; apt-get install -y \
    aufs-tools \
    automake \
    build-essential \
    curl \
    dpkg-sig \
    libcap-dev \
    libsqlite3-dev \
    mercurial \
    reprepro \
    ruby1.9.1 \
    ruby1.9.1-dev \
    s3cmd=1.1.* \
 &amp;&amp; rm -rf /var/lib/apt/lists/*
</span></code></pre>
<p>&#x5176;&#x4E2D; <code>s3cmd</code> &#x6307;&#x4EE4;&#x6307;&#x5B9A;&#x4E86;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#x53F7; <code>1.1.*</code>&#x3002;&#x5982;&#x679C;&#x4E4B;&#x524D;&#x7684;&#x955C;&#x50CF;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x66F4;&#x65E7;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x6307;&#x5B9A;&#x65B0;&#x7684;&#x7248;&#x672C;&#x4F1A;&#x5BFC;&#x81F4; <code>apt-get udpate</code> &#x7F13;&#x5B58;&#x5931;&#x6548;&#x5E76;&#x786E;&#x4FDD;&#x5B89;&#x88C5;&#x7684;&#x662F;&#x65B0;&#x7248;&#x672C;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x6E05;&#x7406;&#x6389; apt &#x7F13;&#x5B58; <code>var/lib/apt/lists</code> &#x53EF;&#x4EE5;&#x51CF;&#x5C0F;&#x955C;&#x50CF;&#x5927;&#x5C0F;&#x3002;&#x56E0;&#x4E3A; <code>RUN</code> &#x6307;&#x4EE4;&#x7684;&#x5F00;&#x5934;&#x4E3A; <code>apt-get udpate</code>&#xFF0C;&#x5305;&#x7F13;&#x5B58;&#x603B;&#x662F;&#x4F1A;&#x5728; <code>apt-get install</code> &#x4E4B;&#x524D;&#x5237;&#x65B0;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5B98;&#x65B9;&#x7684; Debian &#x548C; Ubuntu &#x955C;&#x50CF;&#x4F1A;&#x81EA;&#x52A8;&#x8FD0;&#x884C; apt-get clean&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x9700;&#x8981;&#x663E;&#x5F0F;&#x7684;&#x8C03;&#x7528; apt-get clean&#x3002;</p>
</blockquote>
<h3 id="cmd">CMD</h3>
<p><code>CMD</code> &#x6307;&#x4EE4;&#x7528;&#x4E8E;&#x6267;&#x884C;&#x76EE;&#x6807;&#x955C;&#x50CF;&#x4E2D;&#x5305;&#x542B;&#x7684;&#x8F6F;&#x4EF6;&#xFF0C;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x53C2;&#x6570;&#x3002;<code>CMD</code> &#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#x90FD;&#x5E94;&#x8BE5;&#x4EE5; <code>CMD [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;...]</code> &#x7684;&#x5F62;&#x5F0F;&#x4F7F;&#x7528;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5982;&#x679C;&#x521B;&#x5EFA;&#x955C;&#x50CF;&#x7684;&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x90E8;&#x7F72;&#x67D0;&#x4E2A;&#x670D;&#x52A1;(&#x6BD4;&#x5982; <code>Apache</code>)&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x4F1A;&#x6267;&#x884C;&#x7C7B;&#x4F3C;&#x4E8E; <code>CMD [&quot;apache2&quot;, &quot;-DFOREGROUND&quot;]</code> &#x5F62;&#x5F0F;&#x7684;&#x547D;&#x4EE4;&#x3002;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x4EFB;&#x4F55;&#x670D;&#x52A1;&#x955C;&#x50CF;&#x90FD;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#x7684;&#x547D;&#x4EE4;&#x3002;</p>
<p>&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;<code>CMD</code> &#x90FD;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x4EA4;&#x4E92;&#x5F0F;&#x7684; <code>shell</code> (bash, Python, perl &#x7B49;)&#xFF0C;&#x4F8B;&#x5982; <code>CMD [&quot;perl&quot;, &quot;-de0&quot;]</code>&#xFF0C;&#x6216;&#x8005; <code>CMD [&quot;PHP&quot;, &quot;-a&quot;]</code>&#x3002;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x5F62;&#x5F0F;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x5F53;&#x4F60;&#x6267;&#x884C;&#x7C7B;&#x4F3C; <code>docker run -it python</code> &#x65F6;&#xFF0C;&#x4F60;&#x4F1A;&#x8FDB;&#x5165;&#x4E00;&#x4E2A;&#x51C6;&#x5907;&#x597D;&#x7684; <code>shell</code> &#x4E2D;&#x3002;<code>CMD</code> &#x5E94;&#x8BE5;&#x5728;&#x6781;&#x5C11;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x624D;&#x80FD;&#x4EE5; <code>CMD [&quot;param&quot;, &quot;param&quot;]</code> &#x7684;&#x5F62;&#x5F0F;&#x4E0E; <code>ENTRYPOINT</code> &#x534F;&#x540C;&#x4F7F;&#x7528;&#xFF0C;&#x9664;&#x975E;&#x4F60;&#x548C;&#x4F60;&#x7684;&#x955C;&#x50CF;&#x4F7F;&#x7528;&#x8005;&#x90FD;&#x5BF9; <code>ENTRYPOINT</code> &#x7684;&#x5DE5;&#x4F5C;&#x65B9;&#x5F0F;&#x5341;&#x5206;&#x719F;&#x6089;&#x3002;</p>
<h3 id="expose">EXPOSE</h3>
<p><code>EXPOSE</code> &#x6307;&#x4EE4;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x5BB9;&#x5668;&#x5C06;&#x8981;&#x76D1;&#x542C;&#x7684;&#x7AEF;&#x53E3;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x4E3A;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4F7F;&#x7528;&#x5E38;&#x89C1;&#x7684;&#x7AEF;&#x53E3;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x63D0;&#x4F9B; <code>Apache</code> web &#x670D;&#x52A1;&#x7684;&#x955C;&#x50CF;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>EXPOSE 80</code>&#xFF0C;&#x800C;&#x63D0;&#x4F9B; <code>MongoDB</code> &#x670D;&#x52A1;&#x7684;&#x955C;&#x50CF;&#x4F7F;&#x7528; <code>EXPOSE 27017</code>&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x5728;&#x6267;&#x884C; <code>docker run</code> &#x65F6;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x6807;&#x5FD7;&#x6765;&#x6307;&#x793A;&#x5982;&#x4F55;&#x5C06;&#x6307;&#x5B9A;&#x7684;&#x7AEF;&#x53E3;&#x6620;&#x5C04;&#x5230;&#x6240;&#x9009;&#x62E9;&#x7684;&#x7AEF;&#x53E3;&#x3002;</p>
<h3 id="env">ENV</h3>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x65B0;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>ENV</code> &#x6765;&#x4E3A;&#x5BB9;&#x5668;&#x4E2D;&#x5B89;&#x88C5;&#x7684;&#x7A0B;&#x5E8F;&#x66F4;&#x65B0; <code>PATH</code> &#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3002;&#x4F8B;&#x5982;&#x4F7F;&#x7528; <code>ENV PATH /usr/local/nginx/bin:$PATH</code> &#x6765;&#x786E;&#x4FDD; <code>CMD [&quot;nginx&quot;]</code> &#x80FD;&#x6B63;&#x786E;&#x8FD0;&#x884C;&#x3002;</p>
<p><code>ENV</code> &#x6307;&#x4EE4;&#x4E5F;&#x53EF;&#x7528;&#x4E8E;&#x4E3A;&#x4F60;&#x60F3;&#x8981;&#x5BB9;&#x5668;&#x5316;&#x7684;&#x670D;&#x52A1;&#x63D0;&#x4F9B;&#x5FC5;&#x8981;&#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x6BD4;&#x5982; Postgres &#x9700;&#x8981;&#x7684; <code>PGDATA</code>&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;<code>ENV</code> &#x4E5F;&#x80FD;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x5E38;&#x89C1;&#x7684;&#x7248;&#x672C;&#x53F7;&#xFF0C;&#x6BD4;&#x5982;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">ENV</span> PG_MAJOR <span class="hljs-number">9.3</span>

<span class="hljs-keyword">ENV</span> PG_VERSION <span class="hljs-number">9.3</span>.<span class="hljs-number">4</span>

<span class="hljs-keyword">RUN</span> <span class="bash">curl -SL http://example.com/postgres-<span class="hljs-variable">$PG_VERSION</span>.tar.xz | tar -xJC /usr/src/postgress &amp;&amp; &#x2026;
</span>
<span class="hljs-keyword">ENV</span> PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
</code></pre>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x7A0B;&#x5E8F;&#x4E2D;&#x7684;&#x5E38;&#x91CF;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x53EA;&#x9700;&#x6539;&#x53D8; <code>ENV</code> &#x6307;&#x4EE4;&#x6765;&#x81EA;&#x52A8;&#x7684;&#x6539;&#x53D8;&#x5BB9;&#x5668;&#x4E2D;&#x7684;&#x8F6F;&#x4EF6;&#x7248;&#x672C;&#x3002;</p>
<h3 id="add-&#x548C;-copy">ADD &#x548C; COPY</h3>
<p>&#x867D;&#x7136; <code>ADD</code> &#x548C; <code>COPY</code> &#x529F;&#x80FD;&#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;&#x4E00;&#x822C;&#x4F18;&#x5148;&#x4F7F;&#x7528; <code>COPY</code>&#x3002;&#x56E0;&#x4E3A;&#x5B83;&#x6BD4; <code>ADD</code> &#x66F4;&#x900F;&#x660E;&#x3002;<code>COPY</code> &#x53EA;&#x652F;&#x6301;&#x7B80;&#x5355;&#x5C06;&#x672C;&#x5730;&#x6587;&#x4EF6;&#x62F7;&#x8D1D;&#x5230;&#x5BB9;&#x5668;&#x4E2D;&#xFF0C;&#x800C; <code>ADD</code> &#x6709;&#x4E00;&#x4E9B;&#x5E76;&#x4E0D;&#x660E;&#x663E;&#x7684;&#x529F;&#x80FD;&#xFF08;&#x6BD4;&#x5982;&#x672C;&#x5730; tar &#x63D0;&#x53D6;&#x548C;&#x8FDC;&#x7A0B; URL &#x652F;&#x6301;&#xFF09;&#x3002;&#x56E0;&#x6B64;&#xFF0C;<code>ADD</code> &#x7684;&#x6700;&#x4F73;&#x7528;&#x4F8B;&#x662F;&#x5C06;&#x672C;&#x5730; tar &#x6587;&#x4EF6;&#x81EA;&#x52A8;&#x63D0;&#x53D6;&#x5230;&#x955C;&#x50CF;&#x4E2D;&#xFF0C;&#x4F8B;&#x5982; <code>ADD rootfs.tar.xz</code>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x7684; <code>Dockerfile</code> &#x6709;&#x591A;&#x4E2A;&#x6B65;&#x9AA4;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x4E0D;&#x540C;&#x7684;&#x6587;&#x4EF6;&#x3002;&#x5355;&#x72EC; <code>COPY</code> &#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x6B21;&#x6027;&#x7684; <code>COPY</code> &#x6240;&#x6709;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x5C06;&#x4FDD;&#x8BC1;&#x6BCF;&#x4E2A;&#x6B65;&#x9AA4;&#x7684;&#x6784;&#x5EFA;&#x7F13;&#x5B58;&#x53EA;&#x5728;&#x7279;&#x5B9A;&#x7684;&#x6587;&#x4EF6;&#x53D8;&#x5316;&#x65F6;&#x5931;&#x6548;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">COPY</span> <span class="bash">requirements.txt /tmp/
</span>
<span class="hljs-keyword">RUN</span> <span class="bash">pip install --requirement /tmp/requirements.txt
</span>
<span class="hljs-keyword">COPY</span> <span class="bash">. /tmp/
</span></code></pre>
<p>&#x5982;&#x679C;&#x5C06; <code>COPY . /tmp/</code> &#x653E;&#x7F6E;&#x5728; <code>RUN</code> &#x6307;&#x4EE4;&#x4E4B;&#x524D;&#xFF0C;&#x53EA;&#x8981; <code>.</code> &#x76EE;&#x5F55;&#x4E2D;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x53D8;&#x5316;&#xFF0C;&#x90FD;&#x4F1A;&#x5BFC;&#x81F4;&#x540E;&#x7EED;&#x6307;&#x4EE4;&#x7684;&#x7F13;&#x5B58;&#x5931;&#x6548;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x8BA9;&#x955C;&#x50CF;&#x5C3D;&#x91CF;&#x5C0F;&#xFF0C;&#x6700;&#x597D;&#x4E0D;&#x8981;&#x4F7F;&#x7528; <code>ADD</code> &#x6307;&#x4EE4;&#x4ECE;&#x8FDC;&#x7A0B; URL &#x83B7;&#x53D6;&#x5305;&#xFF0C;&#x800C;&#x662F;&#x4F7F;&#x7528; <code>curl</code> &#x548C; <code>wget</code>&#x3002;&#x8FD9;&#x6837;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6587;&#x4EF6;&#x63D0;&#x53D6;&#x5B8C;&#x4E4B;&#x540E;&#x5220;&#x6389;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x7684;&#x6587;&#x4EF6;&#x6765;&#x907F;&#x514D;&#x5728;&#x955C;&#x50CF;&#x4E2D;&#x989D;&#x5916;&#x6DFB;&#x52A0;&#x4E00;&#x5C42;&#x3002;&#x6BD4;&#x5982;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x4E0B;&#x9762;&#x7684;&#x7528;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">ADD</span> <span class="bash">http://example.com/big.tar.xz /usr/src/things/
</span>
<span class="hljs-keyword">RUN</span> <span class="bash">tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
</span>
<span class="hljs-keyword">RUN</span> <span class="bash">make -C /usr/src/things all
</span></code></pre>
<p>&#x800C;&#x662F;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">RUN</span> <span class="bash">mkdir -p /usr/src/things \
    &amp;&amp; curl -SL http://example.com/big.tar.xz \
    | tar -xJC /usr/src/things \
    &amp;&amp; make -C /usr/src/things all
</span></code></pre>
<p>&#x4E0A;&#x9762;&#x4F7F;&#x7528;&#x7684;&#x7BA1;&#x9053;&#x64CD;&#x4F5C;&#xFF0C;&#x6240;&#x4EE5;&#x6CA1;&#x6709;&#x4E2D;&#x95F4;&#x6587;&#x4EF6;&#x9700;&#x8981;&#x5220;&#x9664;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5176;&#x4ED6;&#x4E0D;&#x9700;&#x8981; <code>ADD</code> &#x7684;&#x81EA;&#x52A8;&#x63D0;&#x53D6;&#x529F;&#x80FD;&#x7684;&#x6587;&#x4EF6;&#x6216;&#x76EE;&#x5F55;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>COPY</code>&#x3002;</p>
<h3 id="entrypoint">ENTRYPOINT</h3>
<p><code>ENTRYPOINT</code> &#x7684;&#x6700;&#x4F73;&#x7528;&#x5904;&#x662F;&#x8BBE;&#x7F6E;&#x955C;&#x50CF;&#x7684;&#x4E3B;&#x547D;&#x4EE4;&#xFF0C;&#x5141;&#x8BB8;&#x5C06;&#x955C;&#x50CF;&#x5F53;&#x6210;&#x547D;&#x4EE4;&#x672C;&#x8EAB;&#x6765;&#x8FD0;&#x884C;&#xFF08;&#x7528; <code>CMD</code> &#x63D0;&#x4F9B;&#x9ED8;&#x8BA4;&#x9009;&#x9879;&#xFF09;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x955C;&#x50CF;&#x63D0;&#x4F9B;&#x4E86;&#x547D;&#x4EE4;&#x884C;&#x5DE5;&#x5177; <code>s3cmd</code>:</p>
<pre><code class="lang-docker"><span class="hljs-keyword">ENTRYPOINT</span> <span class="bash">[<span class="hljs-string">&quot;s3cmd&quot;</span>]
</span>
<span class="hljs-keyword">CMD</span> <span class="bash">[<span class="hljs-string">&quot;--help&quot;</span>]
</span></code></pre>
<p>&#x73B0;&#x5728;&#x76F4;&#x63A5;&#x8FD0;&#x884C;&#x8BE5;&#x955C;&#x50CF;&#x521B;&#x5EFA;&#x7684;&#x5BB9;&#x5668;&#x4F1A;&#x663E;&#x793A;&#x547D;&#x4EE4;&#x5E2E;&#x52A9;&#xFF1A;</p>
<pre><code class="lang-bash">$ docker run s3cmd
</code></pre>
<p>&#x6216;&#x8005;&#x63D0;&#x4F9B;&#x6B63;&#x786E;&#x7684;&#x53C2;&#x6570;&#x6765;&#x6267;&#x884C;&#x67D0;&#x4E2A;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code class="lang-bash">$ docker run s3cmd ls s3://mybucket
</code></pre>
<p>&#x8FD9;&#x6837;&#x955C;&#x50CF;&#x540D;&#x53EF;&#x4EE5;&#x5F53;&#x6210;&#x547D;&#x4EE4;&#x884C;&#x7684;&#x53C2;&#x8003;&#x3002;</p>
<p><code>ENTRYPOINT</code> &#x6307;&#x4EE4;&#x4E5F;&#x53EF;&#x4EE5;&#x7ED3;&#x5408;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x811A;&#x672C;&#x4F7F;&#x7528;&#xFF0C;&#x548C;&#x524D;&#x9762;&#x547D;&#x4EE4;&#x884C;&#x98CE;&#x683C;&#x7C7B;&#x4F3C;&#xFF0C;&#x5373;&#x4F7F;&#x542F;&#x52A8;&#x5DE5;&#x5177;&#x9700;&#x8981;&#x4E0D;&#x6B62;&#x4E00;&#x4E2A;&#x6B65;&#x9AA4;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;<code>Postgres</code> &#x5B98;&#x65B9;&#x955C;&#x50CF;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x811A;&#x672C;&#x4F5C;&#x4E3A; <code>ENTRYPOINT</code>&#xFF1A;</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-built_in">set</span> <span class="hljs-_">-e</span>

<span class="hljs-keyword">if</span> [ <span class="hljs-string">&quot;<span class="hljs-variable">$1</span>&quot;</span> = <span class="hljs-string">&apos;postgres&apos;</span> ]; <span class="hljs-keyword">then</span>
    chown -R postgres <span class="hljs-string">&quot;<span class="hljs-variable">$PGDATA</span>&quot;</span>

    <span class="hljs-keyword">if</span> [ -z <span class="hljs-string">&quot;<span class="hljs-variable">$(ls -A &quot;$PGDATA&quot;)</span>&quot;</span> ]; <span class="hljs-keyword">then</span>
        gosu postgres initdb
    <span class="hljs-keyword">fi</span>

    <span class="hljs-built_in">exec</span> gosu postgres <span class="hljs-string">&quot;<span class="hljs-variable">$@</span>&quot;</span>
<span class="hljs-keyword">fi</span>

<span class="hljs-built_in">exec</span> <span class="hljs-string">&quot;<span class="hljs-variable">$@</span>&quot;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x8BE5;&#x811A;&#x672C;&#x4F7F;&#x7528;&#x4E86; Bash &#x7684;&#x5185;&#x7F6E;&#x547D;&#x4EE4; exec&#xFF0C;&#x6240;&#x4EE5;&#x6700;&#x540E;&#x8FD0;&#x884C;&#x7684;&#x8FDB;&#x7A0B;&#x5C31;&#x662F;&#x5BB9;&#x5668;&#x7684; PID &#x4E3A; 1 &#x7684;&#x8FDB;&#x7A0B;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x8FDB;&#x7A0B;&#x5C31;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x5230;&#x4EFB;&#x4F55;&#x53D1;&#x9001;&#x7ED9;&#x5BB9;&#x5668;&#x7684; Unix &#x4FE1;&#x53F7;&#x4E86;&#x3002;</p>
</blockquote>
<p>&#x8BE5;&#x8F85;&#x52A9;&#x811A;&#x672C;&#x88AB;&#x62F7;&#x8D1D;&#x5230;&#x5BB9;&#x5668;&#xFF0C;&#x5E76;&#x5728;&#x5BB9;&#x5668;&#x542F;&#x52A8;&#x65F6;&#x901A;&#x8FC7; <code>ENTRYPOINT</code> &#x6267;&#x884C;&#xFF1A;</p>
<pre><code class="lang-docker"><span class="hljs-keyword">COPY</span> <span class="bash">./docker-entrypoint.sh /
</span>
<span class="hljs-keyword">ENTRYPOINT</span> <span class="bash">[<span class="hljs-string">&quot;/docker-entrypoint.sh&quot;</span>]
</span></code></pre>
<p>&#x8BE5;&#x811A;&#x672C;&#x53EF;&#x4EE5;&#x8BA9;&#x7528;&#x6237;&#x7528;&#x51E0;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x5F0F;&#x548C; <code>Postgres</code> &#x4EA4;&#x4E92;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5F88;&#x7B80;&#x5355;&#x5730;&#x542F;&#x52A8; <code>Postgres</code>&#xFF1A;</p>
<pre><code class="lang-bash">$ docker run postgres
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x6267;&#x884C; <code>Postgres</code> &#x5E76;&#x4F20;&#x9012;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-bash">$ docker run postgres postgres --help
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x542F;&#x52A8;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x7684;&#x5DE5;&#x5177;&#xFF0C;&#x6BD4;&#x5982; <code>Bash</code>&#xFF1A;</p>
<pre><code class="lang-bash">$ docker run --rm -it postgres bash
</code></pre>
<h3 id="volume">VOLUME</h3>
<p><code>VOLUME</code> &#x6307;&#x4EE4;&#x7528;&#x4E8E;&#x66B4;&#x9732;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x5E93;&#x5B58;&#x50A8;&#x6587;&#x4EF6;&#xFF0C;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x6216;&#x5BB9;&#x5668;&#x521B;&#x5EFA;&#x7684;&#x6587;&#x4EF6;&#x548C;&#x76EE;&#x5F55;&#x3002;&#x5F3A;&#x70C8;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528; <code>VOLUME</code> &#x6765;&#x7BA1;&#x7406;&#x955C;&#x50CF;&#x4E2D;&#x7684;&#x53EF;&#x53D8;&#x90E8;&#x5206;&#x548C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x7684;&#x90E8;&#x5206;&#x3002;</p>
<h3 id="user">USER</h3>
<p>&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x670D;&#x52A1;&#x4E0D;&#x9700;&#x8981;&#x7279;&#x6743;&#x6267;&#x884C;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528; <code>USER</code> &#x6307;&#x4EE4;&#x5207;&#x6362;&#x5230;&#x975E; root &#x7528;&#x6237;&#x3002;&#x5148;&#x5728; <code>Dockerfile</code> &#x4E2D;&#x4F7F;&#x7528;&#x7C7B;&#x4F3C; <code>RUN groupadd -r postgres &amp;&amp; useradd -r -g postgres postgres</code> &#x7684;&#x6307;&#x4EE4;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x548C;&#x7528;&#x6237;&#x7EC4;&#x3002;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x955C;&#x50CF;&#x4E2D;&#xFF0C;&#x7528;&#x6237;&#x548C;&#x7528;&#x6237;&#x7EC4;&#x6BCF;&#x6B21;&#x88AB;&#x5206;&#x914D;&#x7684; UID/GID &#x90FD;&#x662F;&#x4E0D;&#x786E;&#x5B9A;&#x7684;&#xFF0C;&#x4E0B;&#x6B21;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x955C;&#x50CF;&#x65F6;&#x88AB;&#x5206;&#x914D;&#x5230;&#x7684; UID/GID &#x53EF;&#x80FD;&#x4F1A;&#x4E0D;&#x4E00;&#x6837;&#x3002;&#x5982;&#x679C;&#x8981;&#x4F9D;&#x8D56;&#x786E;&#x5B9A;&#x7684; UID/GID&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x663E;&#x793A;&#x7684;&#x6307;&#x5B9A;&#x4E00;&#x4E2A; UID/GID&#x3002;</p>
</blockquote>
<p>&#x4F60;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x4F7F;&#x7528; <code>sudo</code>&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4E0D;&#x53EF;&#x9884;&#x671F;&#x7684; TTY &#x548C;&#x4FE1;&#x53F7;&#x8F6C;&#x53D1;&#x884C;&#x4E3A;&#x53EF;&#x80FD;&#x9020;&#x6210;&#x7684;&#x95EE;&#x9898;&#x6BD4;&#x5B83;&#x80FD;&#x89E3;&#x51B3;&#x7684;&#x95EE;&#x9898;&#x8FD8;&#x591A;&#x3002;&#x5982;&#x679C;&#x4F60;&#x771F;&#x7684;&#x9700;&#x8981;&#x548C; <code>sudo</code> &#x7C7B;&#x4F3C;&#x7684;&#x529F;&#x80FD;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;&#x4EE5; root &#x6743;&#x9650;&#x521D;&#x59CB;&#x5316;&#x67D0;&#x4E2A;&#x5B88;&#x62A4;&#x8FDB;&#x7A0B;&#xFF0C;&#x4EE5;&#x975E; root &#x6743;&#x9650;&#x6267;&#x884C;&#x5B83;&#xFF09;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <a href="https://github.com/tianon/gosu" target="_blank">gosu</a>&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;&#x4E3A;&#x4E86;&#x51CF;&#x5C11;&#x5C42;&#x6570;&#x548C;&#x590D;&#x6742;&#x5EA6;&#xFF0C;&#x907F;&#x514D;&#x9891;&#x7E41;&#x5730;&#x4F7F;&#x7528; <code>USER</code> &#x6765;&#x56DE;&#x5207;&#x6362;&#x7528;&#x6237;&#x3002;</p>
<h3 id="workdir">WORKDIR</h3>
<p>&#x4E3A;&#x4E86;&#x6E05;&#x6670;&#x6027;&#x548C;&#x53EF;&#x9760;&#x6027;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x603B;&#x662F;&#x5728; <code>WORKDIR</code> &#x4E2D;&#x4F7F;&#x7528;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>WORKDIR</code> &#x6765;&#x66FF;&#x4EE3;&#x7C7B;&#x4F3C;&#x4E8E; <code>RUN cd ... &amp;&amp; do-something</code> &#x7684;&#x6307;&#x4EE4;&#xFF0C;&#x540E;&#x8005;&#x96BE;&#x4EE5;&#x9605;&#x8BFB;&#x3001;&#x6392;&#x9519;&#x548C;&#x7EF4;&#x62A4;&#x3002;</p>
<h2 id="&#x5B98;&#x65B9;&#x955C;&#x50CF;&#x793A;&#x4F8B;">&#x5B98;&#x65B9;&#x955C;&#x50CF;&#x793A;&#x4F8B;</h2>
<p>&#x8FD9;&#x4E9B;&#x5B98;&#x65B9;&#x955C;&#x50CF;&#x7684; Dockerfile &#x90FD;&#x662F;&#x53C2;&#x8003;&#x5178;&#x8303;&#xFF1A;<a href="https://github.com/docker-library/docs" target="_blank">https://github.com/docker-library/docs</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="command/dockerd.html" class="navigation navigation-prev " aria-label="Previous page: 服务端命令 - dockerd">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="debug.html" class="navigation navigation-next " aria-label="Next page: 附录五：如何调试 Docker">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"附录四：Dockerfile 最佳实践","level":"1.29.4","depth":2,"next":{"title":"附录五：如何调试 Docker","level":"1.29.5","depth":2,"path":"appendix/debug.md","ref":"appendix/debug.md","articles":[]},"previous":{"title":"服务端命令 - dockerd","level":"1.29.3.2","depth":3,"path":"appendix/command/dockerd.md","ref":"appendix/command/dockerd.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"appendix/best_practices.md","mtime":"2020-04-12T12:29:28.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-23T10:39:04.870Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

